package com.atguigu07.object.equals;

import java.util.Objects;

/**
 * ClassName: UserTest
 * Package: com.atguigu07.object.equals
 * Description:
 *
 * @Author 姜
 * @Create 2023/11/22 16:25
 * @Version 1.0
 */
public class UserTest {
    public static void main(String[] args) {
        User u1 = new User("法师", 25);
        User u2 = new User("法师", 25);
        System.out.println(u1.equals(u2)); // false
        System.out.println(u1 == u2);

        String str1 = new String("hello");
        String str2 = new String("你好");

        System.out.println(str1 == str2); // false
        System.out.println(str1.equals(str2)); // true


    }
}

class User {
    String name;
    int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 重写equals():手动实现

//    @Override
//    public boolean equals(Object obj) {
//        if (this == obj) {
//            return true;
//        }
//        if (obj instanceof User) {
//            User user = (User) obj;
//            // 方式1：
////            if(this.age==user.age&&this.name.equals(user.name)){
////                return true;
////            } else {
////                return false;
////            }
//            return this.age == user.age && this.name.equals(user.name);
//        }
//        return false;
//    }
    // IDEA自动实现

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(name, user.name);
    }

}
